<template>
  <div >
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home/statistics' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>商品列表</el-breadcrumb-item>
  </el-breadcrumb>
    <el-form :inline="true" :model="queryInfo" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input v-model="queryInfo.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="分类ID">
        <el-input v-model="queryInfo.email" placeholder="分类ID"></el-input>
      </el-form-item>
      <el-form-item label="是否上架">
        <el-select v-model="queryInfo.is_on" placeholder="是否上架">
          <el-option label="已上架" :value="1"></el-option>
          <el-option label="未上架" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否推荐">
        <el-select v-model="queryInfo.is_recommend" placeholder="是否推荐">
          <el-option label="已推荐" :value="1"></el-option>
          <el-option label="未推荐" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchGood">查询</el-button>
      </el-form-item>
    </el-form>
  <el-main>
    <el-table :data="goodsList" border  v-loading="loading"  >
      <el-table-column align="center" type="index" label="#" width="80">
      </el-table-column>
      <el-table-column align="center" prop="category_id" label="分类ID" width="80">
      </el-table-column>
      <el-table-column align="center" prop="title" label="名称" width="160">
      </el-table-column>
      <el-table-column align="center" prop="description" label="描述" width="160">
      </el-table-column>
      <el-table-column align="center" prop="price" label="价格" width="80">
      </el-table-column>
      <el-table-column align="center" prop="stock" label="库存" width="80">
      </el-table-column>
      <el-table-column align="center" prop="sales" label="销量" width="80">
      </el-table-column>
      <el-table-column align="center"  label="封面图" width="210">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.cover_url"></el-image>
        </template>
      </el-table-column>
      <el-table-column align="center"   label="上架" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.is_on" @change="changeIsOn(scope.row.id)" :active-value="1" :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center"  label="推荐" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.is_recommend" @change="changeIsRecommend(scope.row.id)" :active-value="1" :inactive-value="0">
          </el-switch>
        </template>
    </el-table-column>
      <el-table-column align="center" prop="updated_at" label="更新时间" width="108">
      </el-table-column>



    </el-table>
    <el-pagination
        style="margin-top: 20px"
        background
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :total="pagination.total">
    </el-pagination>
  </el-main>
    </div>
</template>

<script>
import {getGoodsList,goodsUpDown,goodsRecommend} from "../../api/goods";
export default {
  name: "Statistics",
  data(){
    return{
      goodsList:[],
      pagination:{},
      queryInfo:{
        current:1,
        title:'',
        category_id:null,
        is_on:null,
        is_recommend:null
      },
      loading:false
    }
  },
  mounted() {
    this.getGoodsListData()
  },
  methods:{
    async getGoodsListData(){
      this.loading = true
      const res = await getGoodsList(this.queryInfo)
      if(res && res.status === 200){
        this.loading = false
        this.goodsList = res.data.data
        this.pagination = res.data.meta.pagination
      }
      console.log(res);
    },
    //翻页
    handleCurrentChange(val){
      console.log(val);
      this.queryInfo.current = val
      this.getGoodsListData()
    },
    searchGood(){
      this.getGoodsListData()
    },
    //上下架
    async changeIsOn(id){
      const res = await goodsUpDown(id)
      if(res && res.status === 204){
        this.$message.success('操作成功')
        await this.getGoodsListData()
      }

    },
    async changeIsRecommend(id){
      const res = await goodsRecommend(id)
      if(res && res.status === 204){
        this.$message.success('操作成功')
        await this.getGoodsListData()
      }
    }
  }
}
</script>

<style scoped lang="css">
.el-breadcrumb{
  /*margin-left: 20px;*/
  /*padding:20px 0 0 20px;*/
}
/deep/.el-table__body tr,
.el-table__body td {
  padding: 0;
  height: 40px;
}
.el-form{
  padding:0 0 0 20px;
  margin-top: 20px;
}
</style>